﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using ZhongJia.Lock.Core.Domain;
using ZhongJia.Lock.Infrastructure.Data;
using ZhongJia.Lock.Core.Dto;
using ZhongJia.Lock.Infrastructure.Web;
using Microsoft.EntityFrameworkCore;
using ZhongJia.Lock.Web.Constants;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace ZhongJia.Lock.Web.Controllers
{
    [Route("api/[Controller]")]
    [Authorize()]
    public class CommunityController : BaseController
    {
        private readonly IRepository<Community> _communityRepository;
        private readonly IRepository<LockDevice> _lockDeviceRepository;

        public CommunityController(
            IRepository<Community> communityRepository,
            IRepository<LockDevice> lockDeviceRepository)
        {
            _communityRepository = communityRepository;
            _lockDeviceRepository = lockDeviceRepository;
        }

        /// <summary>
        /// 创建小区
        /// </summary>
        /// <remarks>
        /// params description.
        ///     POST /api/community
        ///     {
        ///        "address": "深圳市南山区银河花园小区", --小区地址
        ///        "name": "银河花园"   --小区名称
        ///     }
        ///     response 
        ///     {
        ///         "CommunityId":guid  --返回小区id
        ///     }
        /// 
        /// </remarks>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Create(CreateCommunityInput input)
        {
            using (var tran = _communityRepository.BeginTransaction())
            {
                var community = new Community
                {
                    Address = input.Address,
                    Name = input.Name,
                };
                _communityRepository.Add(community);
                await _communityRepository.SaveChangeAsync();
                _lockDeviceRepository.Add(new LockDevice
                {
                    Community = community,
                    SerialNumber = $"{community.Number}-{0}-0"
                });
                await _lockDeviceRepository.SaveChangeAsync();
                tran.Commit();
                return Sucessed(new { CommunityId = community.Id });
            }
        }

        /// <summary>
        /// 删除小区
        /// </summary>
        /// <remarks>
        /// params description.
        ///  
        ///     Delete /api/community/{communitId:guid}
        ///     response (statusCode == 200 说明操作成功)
        ///     {
        ///     }
        ///     
        /// </remarks>
        /// <returns></returns>
        [HttpDelete("{communityId:guid}")]
        public async Task<IActionResult> Delete(Guid communityId)
        {
            var community = await _communityRepository.Query().FirstOrDefaultAsync(c => c.Id == communityId);
            if (community == null)
                return Error(Errors.community_not_found.ToString(), "小区id非法");

            community.IsDeleted = true;
            await _communityRepository.SaveChangeAsync();
            return Sucessed();
        }
    }
}
